package modeleditor.editor;

import highlowchart.Highlowchart;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.HighLowRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.MovingAverage;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.date.DateUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A demo showing a high-low-open-close chart with a moving average overlaid on top.
 */
public class drawhighlowchart {
	private static Highlowchart highlowchart;
    /**
     * A demonstration application showing a high-low-open-close chart.
     *
     * @param title  the frame title.
     */
    public drawhighlowchart(Highlowchart highlowchart) {
        this.highlowchart = highlowchart;
    }
    
    /**
     * Creates a sample high low dataset.
     *
     * @return a sample high low dataset.
     */
    public static OHLCDataset createDataset() {
        String[] mydate = new String[highlowchart.getSegments().size()];
        double[] high = new double[highlowchart.getSegments().size()];
        double[] low = new double[highlowchart.getSegments().size()];
        double[] open = new double[highlowchart.getSegments().size()];
        double[] close = new double[highlowchart.getSegments().size()];
        double[] volume = new double[highlowchart.getSegments().size()];
    	Date[] date = new Date[highlowchart.getSegments().size()];
        
        for(int i=0; i<highlowchart.getSegments().size(); i++){
        	mydate[i] = highlowchart.getSegments().get(i).getDate();
        	high[i] = highlowchart.getSegments().get(i).getHigh();
        	low[i] = highlowchart.getSegments().get(i).getLow();
        	open[i] = highlowchart.getSegments().get(i).getOpen();
        	close[i] = highlowchart.getSegments().get(i).getClose();
        	volume[i] = highlowchart.getSegments().get(i).getVolume();
        }
    	
        for(int i=0; i<mydate.length; i++){
        	String[] split = mydate[i].split("-");
        	date[i] = DateUtilities.createDate(Integer.parseInt(split[2]), Integer.parseInt(split[1]), Integer.parseInt(split[0]));
        }

        return new DefaultHighLowDataset(highlowchart.getSeriesname(), date, high, low, open, close, volume);
       
    }   
    
    /**
     * Creates a sample chart.
     * 
     * @param dataset  a dataset.
     * 
     * @return a sample chart.
     */
    public static JFreeChart createChart(OHLCDataset dataset) {
        
        JFreeChart chart = ChartFactory.createHighLowChart(
            highlowchart.getChartName(),
            highlowchart.getXname(), 
            highlowchart.getYname(),
            dataset, 
            true
        );
        XYPlot plot = (XYPlot) chart.getPlot();
        HighLowRenderer renderer = (HighLowRenderer) plot.getRenderer();
        renderer.setOpenTickPaint(Color.red);
        renderer.setCloseTickPaint(Color.black);
        
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
        axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
        //axis.setAutoTickUnitSelection(false);
    	//axis.setAutoRange(false);
        
        // overlay the moving average dataset...
        XYDataset dataset2 = MovingAverage.createMovingAverage(
            dataset, "-MAVG", 3 * 24 * 60 * 60 * 1000L, 0L);
        plot.setDataset(1, dataset2);
        plot.setRenderer(1, new StandardXYItemRenderer());
        
        return chart;
            
    }
     
    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     * 
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }

}


